package com.xqboss.apps.util.mh;


import com.google.common.base.Joiner;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class CommonUtil {

    /**
     * 获取一组BigDecimal 的最大值和最小值
     *
     * @param bigDecimalList
     * @return
     */
    public static Map<String, BigDecimal> getMinAndMax(List<BigDecimal> bigDecimalList) {
        if (bigDecimalList == null || bigDecimalList.isEmpty()) {
            return null;
        }
        Map<String, BigDecimal> map = new HashMap<>();

        BigDecimal minWorth = bigDecimalList.get(0);
        BigDecimal maxWorth = bigDecimalList.get(0);
        for (int i = 0; i < bigDecimalList.size() - 1; i++) {
            BigDecimal currentPrice = bigDecimalList.get(i);
            if (currentPrice.compareTo(minWorth) == -1) {
                minWorth = currentPrice;
            }
            if (currentPrice.compareTo(maxWorth) == 1) {
                maxWorth = currentPrice;
            }
        }
        map.put("min", minWorth);
        map.put("max", maxWorth);
        return map;
    }

    /**
     * 将字符串 转换成list
     *
     * @param str   字符串
     * @param regex 分隔符
     * @return
     */
    public static List<Long> stringToList(String str, String regex) {
        List<Long> list = new ArrayList();
        if (str.isEmpty()) {
            return list;
        }
        List<String> stringList = Arrays.asList(str.split(regex));
        return stringList.stream().map(s -> Long.parseLong(s)).collect(Collectors.toList());
    }


    public static List<String> stringToStringList(String str, String regex) {
        List<String> list = new ArrayList();
        if (str.isEmpty()) {
            return list;
        }
        List<String> stringList = Arrays.asList(str.split(regex));
        return stringList;
    }

    public static List<String> stringToStringList(String str) {
        List<String> list = new ArrayList();
        if (str.isEmpty()) {
            return list;
        }
        List<String> stringList = Arrays.asList(str.split(","));
        return stringList;
    }

    public static <T> String listToString(List<T> tList, String regex) {
        if (tList == null || tList.isEmpty()) {
            return "";
        }
        return Joiner.on(regex).join(tList);
    }


    /**
     * 获取nft编号前缀
     *
     * @return
     */
    public static String getNftPrefix() {
        String letter = getRandomLetter(2);
        int min = 1000;
        int max = 9999;
        int result = new Random().nextInt(max) % (max - min + 1) + min;
        return letter + result;
    }

    /**
     * 获取固定长度随机字母
     *
     * @param length
     * @return
     */
    public static String getRandomLetter(int length) {
        //定义一个整型数组，数组长度为输入值
        int[] arr = new int[length];
        //定义一个Boolean型数组，用来除去重复值
        boolean[] flag = new boolean[26];
        for (int i = 0; i < arr.length; i++) {
            Random rand = new Random();
            int index;
            do {
                //随机生成0-25的数字并赋值给index
                index = rand.nextInt(26);
                //判断flag值是否为true,如果为true则重新为index赋值
            } while (flag[index]);
            //大写字母的ASCII值为65-90，所以给index的值加上65，使其符合大写字母的ASCII值区间
            arr[i] = index + 65;
            flag[index] = true;
        }
        StringBuilder sf = new StringBuilder();
        for (int n = 0; n < arr.length; n++) {
            sf.append((char) (arr[n]));
        }
        return sf.toString();
    }

    public static String getSelfAccretionStr(Integer selfAccretion) {
        if (selfAccretion > 9999) {
            return selfAccretion + "";
        } else {
            String selfAccretionStr = String.valueOf(selfAccretion);
            int zero = 5 - selfAccretionStr.length();
            while (zero > 0) {
                selfAccretionStr = "0" + selfAccretionStr;
                zero--;
            }
            return selfAccretionStr;
        }
    }
}
